﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Security;
using System.Text;

namespace SmartyParty
{
    public partial class sendmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var mail = string.Empty;

            if (Request.QueryString["mail"] != null)
            {
                mail = Request.QueryString["mail"].ToString();
                FormatData(mail);
            }
        }

        private void FormatData(string mail)
        {
            var mailMessage = new MailMessage();
            var name = string.Empty;
            var surname = string.Empty;
            var mobile = string.Empty;
            var email = string.Empty;
            var tel = string.Empty;
            var fax = string.Empty;
            var subject = string.Empty;
            var comments = string.Empty;

            var smtp = new SmtpClient();

            var mailArray = mail.Split(',');
            var dictionaryList = new List<KeyValuePair<string, string>>();

            for (var i = 0; i <= mailArray.Length - 1; i++)
            {
                var keyValue = mailArray[i].Split('|');
                var k = new KeyValuePair<string, string>(keyValue[0], keyValue[1]);
                dictionaryList.Add(k);
            }

            foreach (var key in dictionaryList)
            {
                switch (key.Key.ToLower())
                {
                    case "name":
                        name = key.Value;
                        break;
                    case "surname":
                        surname = key.Value;
                        break;
                    case "mobile":
                        mobile = key.Value;
                        break;
                    case "email":
                        email = key.Value;
                        break;
                    case "tel":
                        tel = key.Value;
                        break;
                    case "fax":
                        fax = key.Value;
                        break;
                    case "subject":
                        subject = key.Value;
                        break;
                    case "comments":
                        comments = key.Value;
                        break;
                }
            }
            SendMailOff(name, surname, mobile, email, tel,fax, subject, comments);
        }


        private void SendMailOff(string name, string surname, string mobile, string email, string tel, string fax, string subject, string comments)
        {
            if (Validate(name, surname, mobile, email))
            {
                try
                {

                var mailMessage = new MailMessage();
                var sender = new MailAddress(email, name);
                var recipient = new MailAddress("info@smartyparty.co.za");                
                var smtp = new SmtpClient();                
                var sb = new StringBuilder();               
                
                    using (var message = new MailMessage()
                    {
                        Body = sb.AppendFormat("{0} {1}<br></br>{2}<br></br>{3}<br></br>{4}<br></br>{5}", name, surname, mobile, tel, fax, comments).ToString(),
                        Subject = subject,
                        From = sender
                    })
                    {
                        message.To.Add(recipient);                        
                        smtp.Send(message);
                    }
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrWhiteSpace(ex.Message))
                        msgDiv.InnerText = string.Format("{0}. {1}", ex.Message, "Please try again later.");

                    msgDiv.Style.Add("color", "red");
                    msgDiv.Style.Add("font-size", "16pt");
                }
            }
            else
            {
                msgDiv.InnerText = "Not all required fields was completed. Please click on contact us Menu again. ";                
            }
        }
        private bool Validate(string name, string surname, string mobile, string email)
        {
            var isValid = true;

            if (string.IsNullOrWhiteSpace(name))
                isValid = false;

            if (string.IsNullOrWhiteSpace(surname))
                isValid = false;

            if (string.IsNullOrWhiteSpace(mobile))
                isValid = false;

            if (string.IsNullOrWhiteSpace(email))
                isValid = false;

            return isValid;
        }

    }

    
}